package m202406.d23;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/6/23 20:55
 */
public class Q134 {

    public static void main(String[] args) {
        Q134 q134 = new Q134();
        q134.canCompleteCircuit(new int[]{1,2,3,4,5}, new int[]{3,4,5,1,2});
    }

    public int canCompleteCircuit(int[] gas, int[] cost) {
        int n = gas.length;
        for (int i = 0; i < n; ) {
            int g = 0;
            int current = 0;
            for (; current < n; current++) {
                int j = (i + current) % n;
                g = g + gas[j] - cost[j];
                if (g < 0) {
                    break;
                }
            }
            if (current == n) {
                return i;
            } else {
                i = i + current + 1;
            }
        }
        return -1;
    }
}
